package com.example.temp.demo;


import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

public class PlaceholderFragment extends Fragment {

	private int counter = 0;
	private ViewGroup argsRows;
	private ViewGroup resultRows;
	private LayoutInflater inflater;

	public PlaceholderFragment() {
	}

	public void addResult() {
		int index = resultRows.getChildCount();
		TextView textView = new TextView(getActivity());
		textView.setPadding(1, 25, 1, 25);
		
		int begin = 0, end = 0;
		SpannableStringBuilder s = new SpannableStringBuilder();
		
		s.append("Result");
		
		begin = s.length();
		s.append(Integer.toString(counter));
		end = s.length();
		s.setSpan(new SuperscriptSpan(), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		
		begin = s.length();
		s.append("Something else");
		end = s.length();
		s.setSpan(new SubscriptSpan(), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		
		s.append(" + Variable");
		
		textView.setText(s);
		//textView.setText(Html.fromHtml("Result<sup>"+counter+"</sup>"+ "<sub><small>"+"Something else"+"</small></sub>"+" + Variable"));
		
		resultRows.addView(textView, index, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		//			resultRows.addView(textView, index);
	}

	public void addArgRow(final ViewGroup argsViewGroup, boolean hideRemoveButton) {
		
		View row = inflater.inflate(R.layout.row, null);
		
		Button b = (Button) row.findViewById(R.id.button1);
		b.setVisibility(hideRemoveButton? View.GONE: View.VISIBLE);
		b.setText(String.valueOf(counter++));
		b.setTag(row);
		b.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				argsViewGroup.removeView((View)v.getTag());
				v.setTag(null);
			}
		});
		
		argsViewGroup.addView(row);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		this.inflater = inflater;
		View rootView = this.inflater.inflate(R.layout.fragment_main, container,false);
		resultRows = (ViewGroup) rootView.findViewById(R.id.result_container);
		argsRows = (ViewGroup) rootView.findViewById(R.id.container);
		
		insertFixedHeader(resultRows);
		insertFixedArgs(argsRows);
		
		return rootView;
	}

	private void insertFixedHeader(ViewGroup resultViewGroup) {
		View view = inflater.inflate(R.layout.header_first, null);
		TextView textView = (TextView) view.findViewById(R.id.title);
		textView.setText("Title of fragment. Some important information here");
		UIHelper.setLayoutParamsWidthMatchHeightWrap(view);
		//view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		resultViewGroup.addView(view);
	}

	private void insertFixedArgs(final ViewGroup argsViewGroup) {
		addArgRow(argsViewGroup,true);
		addArgRow(argsViewGroup,true);
	}

	public void addRemovableArgRow() {
		addArgRow(argsRows, false);
	}
	
}
